package tmproject.hlhj.fhp.tmvote.adapters

import android.graphics.Color
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.StaggeredGridLayoutManager
import android.view.View
import android.widget.TextView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import tmproject.hlhj.fhp.tmvote.Content
import tmproject.hlhj.fhp.tmvote.R
import tmproject.hlhj.fhp.tmvote.beans.PlayerListBean
import tmproject.hlhj.fhp.tmvote.log
import tmproject.hlhj.fhp.tmvote.network.Urls
import tmproject.hlhj.fhp.tmvote.utils.GlideUtils

/**
 * Created by Never Fear   on 2018\8\7 0007.
Never More....
 */
class VotingAdp(private val datas:ArrayList<PlayerListBean.DataBean.ListBean>,private val type:Int)
    :BaseQuickAdapter<PlayerListBean.DataBean.ListBean,BaseViewHolder>(R.layout.voting_item,datas) {
    var listener:OnBtVoteClick?=null
    override fun convert(helper: BaseViewHolder?, item: PlayerListBean.DataBean.ListBean?) {
        helper?.let {
            it.setTextColor(R.id.itemNum,Color.parseColor(Content.vote_num_text_color))
            it.setBackgroundRes(R.id.btVote,Content.vote_commit_bottom_bac)
            val btVote = it.getView<TextView>(R.id.btVote)
            if (type==1){
                it.setText(R.id.btVote,"投票")
                it.getView<TextView>(R.id.tv2).visibility=View.VISIBLE
                it.getView<TextView>(R.id.itemRank).visibility=View.VISIBLE
                it.setVisible(R.id.mask,true)
                it.setVisible(R.id.tvRank,true)
                /*投票点击*/
                log("fhp","设置点击事件")
                btVote.setOnClickListener {
                    log("fhp","点击了？")

                    listener?.btVoteClick(helper?.adapterPosition)
                }
                it.setText(R.id.tvRank,"TOP\n${it.adapterPosition+1}")
            }else{
                it.setText(R.id.btVote,"查看资料")
                it.getView<TextView>(R.id.tv2).visibility=View.GONE
                it.getView<TextView>(R.id.itemRank).visibility=View.GONE
                it.setVisible(R.id.mask,false)
                it.setVisible(R.id.tvRank,false)
                btVote.setOnClickListener {
                    listener?.btVoteClick(helper?.adapterPosition)
                }
            }
            it.setText(R.id.itemName,item?.competitor_name)
            it.setText(R.id.itemRank,item?.ballot.toString())
            it.setText(R.id.itemNum,"NO.${item?.competitor_id}")
            Glide.with(mContext).load(Urls.Base_Url+item?.avatar).apply(GlideUtils.loadOption())
                    .into(it.getView(R.id.itemIcon)!!)

            val itemView = it.itemView
            val layoutParams = itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams
            when(it.adapterPosition%2){
                0->{
                    layoutParams.rightMargin=10
                }
                1->{
                    layoutParams.leftMargin=10
                }
            }
            itemView.layoutParams=layoutParams
        }
    }

    interface OnBtVoteClick{
        fun btVoteClick(position:Int)
    }
}